package com.thejoyrun.retrofit.database;

import com.google.gson.Gson;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by kkmike999 on 2016/05/08.
 *
 * KV数据库
 */
public class DBImpl implements DB {

	/** 模拟kv数据库 */
	public static final Map<String, String> map = new ConcurrentHashMap<>();

	@Override
	public boolean exist(String key) {
		return map.containsKey(key);
	}

	@Override
	public <T> T get(String key, Class<T> clazz) {
		if (map.containsKey(key)) {
			return new Gson().fromJson(map.get(key), clazz);
		}

		return null;
	}

	@Override
	public void put(String key, Object object) {
		map.put(key, new Gson().toJson(object));
	}
}
